<?php
namespace app\home\controller;

use app\api\controller\Alipay;
use app\common\controller\Indexbase;
use app\common\model\OrderShop;
use app\common\model\Order as cxModel;
use app\common\model\UserAddress;

class Order extends Indexbase {
    //  订单支付
    public function playOrder(){
        if(!$this->cxbsuser){
            $this->error("您还没有登录，请先登录",'Login/login');
        }
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        if(empty($getdata['oid'])){
            $this->error("非法访问！",'Index/index');
        }
        $cxmodel = new cxModel();
        $order = $cxmodel->where('oid',$getdata['oid'])->find()->toArray();
        if($order['playstatus'] != '0' && $order['playstatus'] != '4'){
            $this->error('此订单已支付','Index/index');
        }
        if($order['uid'] != $this->cxbsuser['uid']){
            $this->error("非法访问，我们已做报警处理,请及时自首！",'Index/index');
        }
        $webs = array(
            'title' => '订单支付-'.$this->webdb['webname'],
            'keywords' => $this->webdb['webkeywords'],
            'description' => $this->webdb['description'],
        );
        $this->assign([
            'order' => $order,
            'webs' => $webs,
        ]);
        return view($this->temp.'play.htm');
    }
    /**
     *  访问购物车
     */
    public function getChe(){
        if(!$this->cxbsuser){
            $this->error("您还没有登录，请先登录",'Login/login');
        }
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        $getdata['uid'] = $this->cxbsuser['uid'];
        //  获取用户购物车内所有商品
        $shopmodel = new OrderShop();
        $shoplist = $shopmodel->get_list($getdata);
        //  获取用户所有地址
        $ressmodel = new UserAddress();
        $userreslist = $ressmodel->where('uid',$getdata['uid'])->where('status','1')->select();
        $webs = array(
            'title' => '购物车-'.$this->webdb['webname'],
            'keywords' => $this->webdb['webkeywords'],
            'description' => $this->webdb['description'],
        );
        $this->assign([
            'shoplist' => $shoplist,
            'userreslist' => $userreslist,
            'webs' => $webs,
        ]);
        return view($this->temp.'order_che.htm');
    }
    // 下单
    public function addOrder(){
        if(!request()->isPost()){
            $this->error('非法访问');
        }
        $getdata = input('post.');
        $getdata = datatrim($getdata);
        foreach ($getdata['end'] as $k => $v){
            $shop[] = $getdata['aids'][$v];
        }
        $class = empty($getdata['class']) ? '0' : $getdata['class'];
        $cxmodel = new cxModel();
        $getdata['shop'] = $shop;
        $getdata['class'] = $class;
        $res = $cxmodel->add_order($getdata);
        //  清空购物车
        if($res['code'] != '0'){
            $shopmodel = new OrderShop();
            $shopmodel->alldel($getdata['id']);
        }
        return json($res);
    }
    //  订单支付开始
    public function orderPay(){
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        $cxmodel = new cxModel();
        $order = $cxmodel->where('oid',$getdata['oid'])->find()->toArray();
        if($order['playstatus'] != '0' && $order['playstatus'] != '4'){
            $this->error('此订单已支付','Index/index');
        }
        if($order['uid'] != $this->cxbsuser['uid']){
            $this->error('非法访问！我们已记录你的信息并报警,请及时自首！','Index/index');
        }
        $order['loid'] = $this->linshioid($order['oid']);
        $webs = array(
            'title' => '订单支付-'.$this->webdb['webname'],
            'keywords' => $this->webdb['webkeywords'],
            'description' => $this->webdb['description'],
        );
        $this->assign([
            'order' => $order,
            'webs' => $webs,
        ]);
        if($getdata['cid'] == 'ali'){
            $alipaymodel = new Alipay();
            $alipaymodel->get_pcpay($order); // 未开通扫码支付，使用电脑支付
//            $erweima = $alipaymodel->get_saoma($order);
//            if($erweima != false){
//                $this->assign('erweima',$erweima);
//                return view($this->temp.'aliplay.htm');
//            }else{
//                $this->error("订单错误！请重试！");
//            }
        }elseif ($getdata['cid'] == 'wx'){
            $erweima = controller('Wxpay')->get_saoma($order);
            if($erweima !== false){
                $this->assign('erweima',$erweima);
                return view($this->temp.'wxplay.htm');
            }else{
                $this->error('唤起微信支付失败，请重新提交订单！');
            }
        }
    }

    //  生成临时订单号并存入数据库
    public function linshioid($oid){
        $numbers = range (1,50);
        shuffle ($numbers);
        $num=8;
        $result = array_slice($numbers,0,$num);
        $result = implode('',$result);
        $newname = 'LSPAY'.date("Ymd");
        for ( $i = 0; $i < 8; $i++ ) {
            $newname .= substr($result, mt_rand(0, strlen($result) - 1), 1);
        }
        $data['oid'] = $oid;
        $data['lsoid'] = $newname;
        model('OrderPlay')->save($data);
        return $newname;
    }
}
